package com.yunc.modules.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yunc.base.controller.CommonController;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.yunc.common.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.modules.sys.entity.SysSendSms;
import com.yunc.modules.sys.service.ISysSendSmsService;

import java.util.List;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;

/**
 * 短信通知记录表 前端控制器
 *
 * @author concong tao
 * @since 2019-02-17
 */
@RestController
@RequestMapping("/sysSendSms")
public class SysSendSmsController extends CommonController {

    @Autowired
    private ISysSendSmsService sysSendSmsService;

    @GetMapping("/page")
    public R page(Page<SysSendSms> page, SysSendSms sysSendSms){
        QueryWrapper<SysSendSms> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysSendSms.getPhone())) {
            qw.eq("phone", sysSendSms.getPhone());
        }
        sysSendSmsService.page(page, qw);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        SysSendSms sysSendSms = sysSendSmsService.getById(id);
        return R.ok().put("sysSendSms", sysSendSms);
    }

    @PostMapping("/save")
    public R save(@RequestBody SysSendSms sysSendSms){
        // 发送邮件
        boolean flag = sysSendSmsService.snedAndSave(sysSendSms);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody SysSendSms sysSendSms){
        boolean flag = sysSendSmsService.updateById(sysSendSms);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids){
        if (StringUtils.isEmpty(ids)) {
            return R.error("传入值为空");
        }
        List<String> idsList = StrSpliter.split(ids, ',', 0, true, true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = sysSendSmsService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
